package com.hsd.znsh.controller.pc;

import com.hsd.znsh.code.ValidateCodeProcessor;
import com.hsd.znsh.code.ValidateCodeProcessorHolder;
import com.hsd.znsh.entity.FarmType;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.pojo.input.PCRegisterUser;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.properties.SecurityProperties;
import com.hsd.znsh.service.CommonService;
import com.hsd.znsh.service.FarmService;
import com.hsd.znsh.service.FarmTypeService;
import com.hsd.znsh.service.UserService;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-23 11:25:24
 */
@Controller
@RequestMapping("/pc/user")
public class PCUserController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private UserService userService;

    @Autowired
    private ValidateCodeProcessorHolder validateCodeProcessorHolder;

    @Autowired
    private FarmTypeService farmTypeService;

    @Autowired
    private FarmService farmService;

    @GetMapping("/login")
    public String toLogin(){
        return "pc/login";
    }

    @GetMapping("/register")
    public String toRegister(){
        return "pc/register";
    }

    @RequestMapping("/main")
    public String toMain(HttpServletRequest request){
        request.setAttribute("username",SecurityUtil.getUsername());
        return "pc/map";
    }

    @GetMapping("/forgetPassword")
    public String toForgetPassword(){
        return "pc/forgetPassword";
    }


    @PostMapping("/sendSmsCode")
    @ResponseBody
    public void sendSmsCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ValidateCodeProcessor validateCodeProcessor=validateCodeProcessorHolder.findValidateCodeProcessor("sms");
        validateCodeProcessor.create(new ServletWebRequest(request, response));
    }

    @PostMapping("/register")
    @ResponseBody
    public ResultVO register(@Valid PCRegisterUser pcRegisterUser, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        userService.registerPCUser(pcRegisterUser);
        return ResultVOUtil.success("注册成功");
    }

    @GetMapping("/getUserRole")
    @ResponseBody
    public ResultVO getUserRole(){
        return ResultVOUtil.success(commonService.getUserRole(SecurityUtil.getUser()));
    }

    @GetMapping("/getFarmType")
    @ResponseBody
    public ResultVO getFarmType(){
        List<FarmType> list=farmTypeService.getFarmTypeList();
        return ResultVOUtil.success(list);
    }


    @GetMapping("/getFarmRecord")
    @ResponseBody
    public ResultVO getFarmRecord(Integer farmTypeId,Integer totalTime){
        List<Map<String,Object>> list=farmService.getFarmRecord(farmTypeId,totalTime);
        return ResultVOUtil.success(list);
    }
}
